package com.jrh.web.member.tools;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.jrh.common.cookie.UserCookieManager;
import com.jrh.common.util.DateUtils;
import com.jrh.domain.entity.User;
import com.jrh.domain.user.LoginUserInfo;

/**
 * 用户授权工具
 * 
 * @author jasen.hsu.cn@gmail.com 2012-7-19 下午5:31:37
 */
public class UserAuthUtil {

    private static final String USER_DEFAULT_FACE_URL = "/img/user_default_face.jpg";

    /**
     * 取得当前登录用户信息，未登录返回null
     * 
     * @param request
     * @param response
     * @return
     */
    public static LoginUserInfo getUserInfo(HttpServletRequest request, HttpServletResponse response) {
        LoginUserInfo loginUserInfo = null;
        UserCookieManager userCookieManager = new UserCookieManager(request, response, null, "/");
        String userId = userCookieManager.getTempCookie(UserCookieManager.USER_ID, null);
        if (StringUtils.isNotBlank(userId)) {
            loginUserInfo = new LoginUserInfo();
            loginUserInfo.setId(Long.valueOf(userId));
            loginUserInfo.setLoginId(userCookieManager.getTempCookie(UserCookieManager.USER_LOGIN_ID, null));
            loginUserInfo.setSite(userCookieManager.getTempCookie(UserCookieManager.USER_SITE, null));
            loginUserInfo.setNickname(userCookieManager.getTempCookie(UserCookieManager.USER_NICK, null));
            loginUserInfo.setType(userCookieManager.getTempCookie(UserCookieManager.USER_TYPE, null));
            loginUserInfo.setLastLoginTime(userCookieManager.getTempCookie(UserCookieManager.LAST_LOGIN_TIME, null));
            loginUserInfo.setFaceUrl(userCookieManager.getTempCookie(UserCookieManager.USER_FACE_URL,
                                                                     USER_DEFAULT_FACE_URL));
        }
        return loginUserInfo;
    }

    /**
     * 取得当前登录用户标识，未登录返回null
     * 
     * @param request
     * @param response
     * @return
     */
    public static Long getUserId(HttpServletRequest request, HttpServletResponse response) {
        UserCookieManager userCookieManager = new UserCookieManager(request, response, null, "/");
        String userId = userCookieManager.getTempCookie(UserCookieManager.USER_ID, null);
        if (StringUtils.isNotBlank(userId)) {
            return Long.valueOf(userId);
        }
        return null;
    }

    /**
     * 判断当前是否登录状态
     * 
     * @param request
     * @param response
     * @return
     */
    public static boolean isSignIn(HttpServletRequest request, HttpServletResponse response) {
        UserCookieManager userCookieManager = new UserCookieManager(request, response, null, "/");
        String userId = userCookieManager.getTempCookie(UserCookieManager.USER_ID, null);
        return StringUtils.isNotBlank(userId);
    }

    /**
     * 登录，写cookie
     * 
     * @param loginUserInfo
     * @param request
     * @param response
     */
    public static void signIn(LoginUserInfo loginUserInfo, HttpServletRequest request, HttpServletResponse response) {
        if (loginUserInfo != null && loginUserInfo.getId() != null) {
            UserCookieManager userCookieManager = new UserCookieManager(request, response, null, "/");
            userCookieManager.setTempCookie(UserCookieManager.USER_ID, "" + loginUserInfo.getId());
            userCookieManager.setTempCookie(UserCookieManager.USER_LOGIN_ID, loginUserInfo.getLoginId());
            userCookieManager.setTempCookie(UserCookieManager.USER_SITE, loginUserInfo.getSite());
            userCookieManager.setTempCookie(UserCookieManager.USER_NICK, loginUserInfo.getNickname());
            userCookieManager.setTempCookie(UserCookieManager.USER_TYPE, loginUserInfo.getType());
            userCookieManager.setTempCookie(UserCookieManager.LAST_LOGIN_TIME, DateUtils.dateConvert2String(new Date()));
            userCookieManager.setTempCookie(UserCookieManager.USER_FACE_URL, loginUserInfo.getFaceUrl());
            userCookieManager.saveTempCookie();
        }
    }

    /**
     * 登录，写cookie
     * 
     * @param user
     * @param request
     * @param response
     */
    public static void signIn(User user, HttpServletRequest request, HttpServletResponse response) {
        if (user != null && user.getId() != null) {
            UserCookieManager userCookieManager = new UserCookieManager(request, response, null, "/");
            userCookieManager.setTempCookie(UserCookieManager.USER_ID, user.getId() + "");
            userCookieManager.setTempCookie(UserCookieManager.USER_LOGIN_ID, user.getLoginId());
            userCookieManager.setTempCookie(UserCookieManager.USER_SITE, user.getSite());
            userCookieManager.setTempCookie(UserCookieManager.USER_NICK, user.getNickname());
            userCookieManager.setTempCookie(UserCookieManager.USER_TYPE, user.getUserType());
            userCookieManager.setTempCookie(UserCookieManager.LAST_LOGIN_TIME, DateUtils.dateConvert2String(new Date()));
            userCookieManager.setTempCookie(UserCookieManager.USER_FACE_URL, user.getFaceUrl());
            userCookieManager.saveTempCookie();
        }

    }

    /**
     * 登出
     * 
     * @param request
     * @param response
     */
    public static void signOut(HttpServletRequest request, HttpServletResponse response) {
        UserCookieManager userCookieManager = new UserCookieManager(request, response, null, "/");
        userCookieManager.setTempCookie(UserCookieManager.USER_ID, null);
        userCookieManager.removeTempCookie(UserCookieManager.USER_ID);
        userCookieManager.setTempCookie(UserCookieManager.USER_LOGIN_ID, null);
        userCookieManager.removeTempCookie(UserCookieManager.USER_LOGIN_ID);
        userCookieManager.setTempCookie(UserCookieManager.USER_SITE, null);
        userCookieManager.removeTempCookie(UserCookieManager.USER_SITE);
        userCookieManager.setTempCookie(UserCookieManager.USER_NICK, null);
        userCookieManager.removeTempCookie(UserCookieManager.USER_NICK);
        userCookieManager.setTempCookie(UserCookieManager.USER_TYPE, null);
        userCookieManager.removeTempCookie(UserCookieManager.USER_TYPE);
        userCookieManager.setTempCookie(UserCookieManager.LAST_LOGIN_TIME, null);
        userCookieManager.removeTempCookie(UserCookieManager.LAST_LOGIN_TIME);
        userCookieManager.setTempCookie(UserCookieManager.USER_FACE_URL, null);
        userCookieManager.removeTempCookie(UserCookieManager.USER_FACE_URL);
        userCookieManager.saveTempCookie();
    }

}
